package com.itcrx.datastructure;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @author NoobMaster69  * @date 2024/6/14 0014 星期五 17:53
 */

/**
 * 单向链表带哨兵
 */
public class SinglyLinkedListSentinel implements Iterable<Integer> {
    private Node head = new Node(666,null);  //头指针

    /**
     * 遍历方式3 迭代器
     *
     * @return
     */
    @Override
    public Iterator<Integer> iterator() {
        return new NoteIterator();
    }

    private class NoteIterator implements Iterator<Integer> {
        Node p = head.next;

        @Override
        public boolean hasNext() {
            return p != null;
        }

        @Override
        public Integer next() {
            int v = p.value;
            p = p.next;
            return v;
        }
    }

    /**
     * 节点类
     */

    private static class Node {
        private int value;  //值
        private Node next;  //指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 向链表头部添加节点
     *
     * @param value 待添加值
     */
    public void addFirst(int value) {
        insert(0,value);
    }

    /**
     * 遍历链表
     */
    public void loop1(Consumer<Integer> consumer) {
        Node p = head.next;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    /**
     * 遍历数组2
     *
     * @param consumer 要执行的操作
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    /**
     * 查找最后一个节点
     *
     * @return 返回null就是空链表
     */
    private Node findLast() {
        Node p;
        for (p = head; p.next != null; p = p.next) {

        }
        return p;
    }

    /**
     * 向节点最后添加节点
     *
     * @param value 要添加的值
     */
    public void addLast(int value) {
        Node last = findLast();
        last.next = new Node(value, null);
    }

    /**
     * 查找索引节点
     *
     * @param index 索引
     * @return 返回节点
     */
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;    //没找到
    }

    /**
     * 根据索引查找节点值
     *
     * @param index 索引
     * @return 找到返回索引节点的值
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw IllegalIndex(index);
        }
        return node.value;
    }

    /**
     * 向索引位置插入
     *
     * @param index 索引
     * @param value 待插入值
     */
    public void insert(int index, int value) {
        //如果index=0的情况下，直接在头部
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw IllegalIndex(index);
        }
        prev.next = new Node(value, prev.next);
    }

    private static IllegalArgumentException IllegalIndex(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不存在%n", index));
    }

    /**
     * 删除第一个节点
     */
    public void removeFirst() throws IllegalArgumentException{
        remove(0);
    }

    /**
     * 删除索引的节点
     * @param index 索引
     */
    public void remove(int index) throws IllegalArgumentException{
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw IllegalIndex(index);
        }
        Node removed = findNode(index);
        if (removed == null){
            throw IllegalIndex(index);
        }
        prev.next = removed.next;
    }
}


